<html>
<head><meta charset="utf-8"><title>Chaining generic iterators · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Chaining.20generic.20iterators.html">Chaining generic iterators</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229509939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Chaining%20generic%20iterators/near/229509939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sean Klein <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Chaining.20generic.20iterators.html#229509939">(Mar 09 2021 at 16:49)</a>:</h4>
<p>Heya, I'm trying to add a prefix to a generic iterator, and I'm a little stumped by the compiler message:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">use_iterator</span><span class="p">(</span><span class="n">args</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">IntoIterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">AsRef</span><span class="o">&lt;</span><span class="kt">str</span><span class="o">&gt;&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">extend</span><span class="p">(</span><span class="n">suffix</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">IntoIterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">AsRef</span><span class="o">&lt;</span><span class="kt">str</span><span class="o">&gt;&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">prefix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="s">"hello"</span><span class="p">.</span><span class="n">to_string</span><span class="p">()];</span><span class="w"></span>
<span class="w">    </span><span class="n">use_iterator</span><span class="p">(</span><span class="n">prefix</span><span class="p">.</span><span class="n">into_iter</span><span class="p">().</span><span class="n">chain</span><span class="p">(</span><span class="n">suffix</span><span class="p">));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I'm seeing the following error on the <code>chain</code> invocation: "expected type parameter <code>impl AsRef&lt;str&gt;</code>, found <code>&amp;String</code>".<br>
Doesn't <code>&amp;String</code> implement <code>AsRef&lt;str&gt;</code>? What's the right way to do this chaining?</p>



<a name="229509987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Chaining%20generic%20iterators/near/229509987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sean Klein <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Chaining.20generic.20iterators.html#229509987">(Mar 09 2021 at 16:49)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=dcd686dbc22870346bbc5d4a8bd81186">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=dcd686dbc22870346bbc5d4a8bd81186</a></p>



<a name="229511997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Chaining%20generic%20iterators/near/229511997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Chaining.20generic.20iterators.html#229511997">(Mar 09 2021 at 17:00)</a>:</h4>
<p><code>impl Foo</code> is just sugar for <code>... where T: Foo</code>. I.e. it's generic. So you're trying to chain an iterator over <code>String</code> items together with an iterator over any type as long as it implements <code>AsRef&lt;str&gt;</code>.</p>
<p>Chain's signature requires them to be _identical_, which is more strict than merely implementing the same traits. U::Item = Self::Item</p>
<div class="codehilite"><pre><span></span><code>fn chain&lt;U&gt;(self, other: U) -&gt; Chain&lt;Self, U::IntoIter&gt;ⓘ where
    Self: Sized,
    U: IntoIterator&lt;Item = Self::Item&gt;,
</code></pre></div>
<p>If you wanted heterogenous iterators you'd have to work with dyn Trait. Or you have to normalize them first to <code>Item=&amp;str</code></p>



<a name="229549353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Chaining%20generic%20iterators/near/229549353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Chaining.20generic.20iterators.html#229549353">(Mar 09 2021 at 20:39)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">extend</span><span class="p">(</span><span class="n">suffix</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">IntoIterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">AsRef</span><span class="o">&lt;</span><span class="kt">str</span><span class="o">&gt;&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">prefix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="s">"hello"</span><span class="p">.</span><span class="n">to_string</span><span class="p">()];</span><span class="w"></span>
<span class="w">    </span><span class="n">use_iterator</span><span class="p">(</span><span class="n">prefix</span><span class="p">.</span><span class="n">into_iter</span><span class="p">().</span><span class="n">chain</span><span class="p">(</span><span class="n">suffix</span><span class="p">));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>desugars to</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">extend</span><span class="o">&lt;</span><span class="n">I</span><span class="o">&gt;</span><span class="p">(</span><span class="n">suffix</span>: <span class="nc">I</span><span class="p">)</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">I</span>: <span class="nb">IntoIterator</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">I</span>::<span class="n">Item</span>: <span class="nb">AsRef</span><span class="o">&lt;</span><span class="kt">str</span><span class="o">&gt;</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">prefix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="s">"hello"</span><span class="p">.</span><span class="n">to_string</span><span class="p">()];</span><span class="w"></span>
<span class="w">    </span><span class="n">use_iterator</span><span class="p">(</span><span class="n">prefix</span><span class="p">.</span><span class="n">into_iter</span><span class="p">().</span><span class="n">chain</span><span class="p">(</span><span class="n">suffix</span><span class="p">));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>which means that the type of the item is determined by the caller of <code>extend</code> and you don't know inside of <code>extend</code>what type it'll be.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>